package com.foreveross.crawl.workers;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.foreveross.crawl.application.impl.computer.ComputerInstanceByStrategy;
import com.foreveross.crawl.common.cfg.PropertyUtils;
import com.foreveross.crawl.common.cfg.system.SystemPropertiesLoader;
import com.foreveross.crawl.vo.MachineInfo;
import com.foreveross.crawl.vo.Message;
import com.foreveross.util.GlobalVariable;
import com.foreveross.util.HttpClientUtil;
import com.foreveross.util.NodeInfoPropertyUtil;
import com.foreveross.util.PropertesCustom;

/**
 * 发送心跳包类
 * @author luofangyi
 * 
 */
public class SendHeartbeatWorker implements Job {

	public static Logger logger = Logger.getLogger(SendHeartbeatWorker.class);

	@Override
	public void execute(JobExecutionContext context)
			throws JobExecutionException {

		logger.info("发送心跳任务开始");
		String ipRemoteUrl = PropertyUtils.getProperty(
				PropertesCustom.SYSTEM_REMOTE_IP_URL,
				SystemPropertiesLoader.FILE_NAME, "");
		String headUrl = PropertyUtils.getProperty(
				PropertesCustom.SYSTEM_HEART_URL,
				SystemPropertiesLoader.FILE_NAME, "");
		
		String nodeSn = NodeInfoPropertyUtil.getValueByProperty(NodeInfoPropertyUtil.NODE_SN);
		try {
			HttpClientUtil httpClient = HttpClientUtil.getInstance();
			httpClient.executeByGet(ipRemoteUrl + headUrl + nodeSn + GlobalVariable.suffix_request);
		} catch (Exception e2) {
			logger.error(e2.getMessage());
		}
		logger.info("发送心跳任务结束");
	}

	private String buiderNodeSn() {
		String nodeSn = "";
		if (NodeInfoPropertyUtil
				.getValueByProperty(NodeInfoPropertyUtil.NODE_SN) == null) {
			nodeSn = java.util.UUID.randomUUID().toString();
			NodeInfoPropertyUtil.writeProperty(NodeInfoPropertyUtil.NODE_SN,
					nodeSn);
		} else {
			nodeSn = NodeInfoPropertyUtil
					.getValueByProperty(NodeInfoPropertyUtil.NODE_SN);
		}
		return nodeSn;
	}

	private String builderNodeSn(Message message){
		if(message == null){
			return "";
		}
		String port = PropertyUtils.getProperty(
				PropertesCustom.SYSTEM_PORT,
				SystemPropertiesLoader.FILE_NAME, "8080");
		
		String nodeName = NodeInfoPropertyUtil.getValueByProperty(NodeInfoPropertyUtil.NODE_NAME, "node1");
		MachineInfo machineInfo = ((List<MachineInfo>)message.getBody()).get(0);
		boolean obtainIp = PropertyUtils.getBooleanProperty(PropertesCustom.SYSTEM_NET_OBTAIN_IP, SystemPropertiesLoader.FILE_NAME);
		String ip = "";
		if(obtainIp){//是否从联网获取IP
			try {
				ip = obtainNetIp();
			} catch (Exception e) {
				logger.error(e.getMessage());
			}
		}
		if(ip == ""){
			ip = machineInfo.getIp();
		}
		
		String nodeSn = ip + port + nodeName;
		return nodeSn;
	}

	private String obtainNetIp() throws Exception {

		String url = "http://20140507.ip138.com/ic.asp";
		HttpClientUtil httpClient = HttpClientUtil.getInstance();
		String responseMsg = httpClient.executeByGet(url);
		String regEx = "\\[(.+?)\\]";
		return getPatternString(responseMsg, regEx);
	}

	private String getPatternString(String content, String regEx) {
		Pattern p = Pattern.compile(regEx, Pattern.DOTALL | Pattern.MULTILINE);
		Matcher m = p.matcher(content);
		String retStr = "";
		if (m.find()) {
			retStr = m.group(1);
		}
		return retStr.trim().replaceAll("&nbsp;", "");
	}
}
